import { fromJS } from 'immutable'
import * as types from './action-types'

const defaultState = fromJS({
  focused: false,
  searchList: [],
  mouseIn: false,
  page: 1,
  totalPage: 1
})

export default (state = defaultState, action) => {
  switch (action.type) {
    case types['SEARCH_FOCUS']:
      return state.set('focused', true)
    case types['SEARCH_BLUR']:
      return state.set('focused', false)
    case types['SET_SEARCH_LIST']:
      const newList = action.value.toJS()
      return state.merge({
        'searchList': action.value,
        'totalPage': Math.ceil(newList.length / 10)
      })
    case types['SET_SEARCH_MOUSEIN']:
      return state.set('mouseIn', action.value)
    case types['SET_SEARCH_PAGE']:
      return state.set('page', action.value)
    default:
      return state
  }

}
